package com.ruoyi.platform.system.user.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.base.utils.oss.OssUtils;
import com.ruoyi.platform.system.role.pojo.RoleDTO;
import com.ruoyi.platform.system.role.pojo.UserRoleDO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* 用户信息表
* @author lcf
* @version 0.0.1
* @since 2020-11-13 05:40:22
*/
@Data
@NoArgsConstructor
@ApiModel
public class UserDTO {
    /**
     * 用户id
     */
    @ApiModelProperty("用户id")
    private Long userId;
    /**
     * 账号
     */
    @ApiModelProperty("账号")
    private String account;
    /**
     * 昵称
     */
    @ApiModelProperty("昵称")
    private String nickName;
    /**
     * 手机号码
     */
    @ApiModelProperty("手机号码")
    private String phone;
    /**
     * 头像
     */
    @ApiModelProperty("头像")
    private String headImg;
    /**
     * 账号状态（0正常 1停用）
     */
    @ApiModelProperty("账号状态（0正常 1停用）")
    private Integer accountStatus;
    /**
     * 角色列表
     */
    @ApiModelProperty("角色列表")
    private List<RoleDTO> roleList;
    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    public UserDTO(UserDO info) {
        if(info != null) {
            this.userId = info.getUserId();
            this.account = info.getAccount();
            this.nickName = info.getNickName();
            this.phone = info.getPhone();
            this.headImg = OssUtils.getAbsoluteUrl(info.getHeadImg());
            this.createTime = info.getCreateTime();
            this.accountStatus = info.getAccountStatus();
        }
    }

    public UserDTO(UserDO info, List<UserRoleDO> userRoleList) {
        if(info != null) {
            this.userId = info.getUserId();
            this.account = info.getAccount();
            this.nickName = info.getNickName();
            this.phone = info.getPhone();
            this.headImg = OssUtils.getAbsoluteUrl(info.getHeadImg());
            this.createTime = info.getCreateTime();
            this.accountStatus = info.getAccountStatus();
            if (CollectionUtils.isNotEmpty(userRoleList)) {
                this.roleList = userRoleList.stream().filter(x -> Objects.equals(info.getUserId(), x.getUserId()))
                        .map(UserRoleDO::getRoleCode).map(RoleDTO::new).collect(Collectors.toList());
            }
        }
    }
}